/*
Table_01. Summary statistics
Table_02. Productivity and sentiment: Full sample
Table_03. Productivity and sentiment: G7 breakdown
Table_04. Productivity and sentiment: Financial development
Table_05. Sentiment and economic growth: G7 countries
Table_06. Sentiment and economic growth: Non-G7 countries
Table_07. Future stock returns
Table_08. Equity and credit markets

NOTE: The definition of TFP growth (or "RTFP") is in Eq.(5) of "capital_labor_and_tfp_in_pwt80"

Bootstrapping notes:
- see link here -> https://www.stata.com/support/faqs/statistics/bootstrap-with-panel-data/
- see articles in bootstrapping folder (under references)
*/

* Table_01. Summary statistics
	clear all
	use "data/final_data_set.dta"
	xtset id year

	// Panel A. Full sample
		tabstat ///
		rtfpna pd cci ///
		g_rgdpna g_emp g_rhc g_rgcf irr if cci_ort != ., ///
		columns(statistics) statistics(mean sd p25 p50 p75) format(%9.4f)

	// Panel B. G7 countries
		tabstat ///
		rtfpna pd cci ///
		g_rgdpna g_emp g_rhc g_rgcf irr if cci_ort != . & dum_G7 == 1, ///
		columns(statistics) statistics(mean sd p25 p50 p75) format(%9.4f)
		
	// Panel C. Non-G7 countries
		tabstat ///
		rtfpna pd cci ///
		g_rgdpna g_emp g_rhc g_rgcf irr if cci_ort != . & dum_G7 == 0, ///
		columns(statistics) statistics(mean sd p25 p50 p75) format(%9.4f)
		
	// list of countries in the sample	
		tabulate country if cci_ort != . & pd != . // 17 overall
			// list: Australia, Austria, Belgium, Canada (new), Denmark, Finland, France, Germany, Ireland, Italy, Netherlands, New Zealand, Spain, Sweden, Switzerland, United Kingdom, United States.
	
	// sample period
		tabstat year if cci_ort != . & pd != . & cou != "USA", statistics(min max) // 1975-2019
				
	// pd and cci_ort across G7 and non-G7 countries
		reg rtfpna dum_G7 if cci_ort != .
		reg pd dum_G7 if cci_ort != .
		reg cci_ort dum_G7 if pd != .
	
	// correlations between PD and sentiment measures
		foreach x in pd {
		gen ln_`x' = ln(`x')
		}
		reg ln_pd zcci_ort zcci_exp
				
	// distribution of FD in the sample (no outliers)
		tabulate cou if cci != . & pd != ., summarize (fd2)
		tabulate cou if cci != . & pd != ., summarize (fd0)
		tabstat fd2 if cci != . & pd != ., statistics(mean sd min p25 p50 p75 max)
		
	// correlation between sentiment, G7 dummy, and FD
		pwcorr cci_ort dum_G7 if pd != ., sig
		pwcorr cci_ort zfd2 if pd != ., sig
		pwcorr dum_G7 zfd2 if pd != ., sig
		
	// correlation between sentiment measured as an average v. December only
		pwcorr cci cci_m12, sig
		pwcorr cci_exp cci_exp_m12, sig
		pwcorr cci_ort cci_ort_m12, sig

	// list of countries
		tabulate country if zcci != . & rtfpna != .
		// for reference, OECD members are here: https://data.worldbank.org/country/OE
	

* Table_02. Productivity and sentiment: Full sample
	clear all
	use "data/final_data_set.dta"
	xtset id year
	
	foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
		qui: regxfe `x' zcci zd_* zF*, robust fe(id) cluster(year)
			eststo
	}
	esttab using "Table_02.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_02) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
	drop()
	estimates clear

	
* Table_03. Productivity and sentiment: G7 breakdown
	// G7 subsample
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
			qui: regxfe `x' zcci zd_* zF* if dum_G7 == 1, robust fe(id) cluster(year)
				eststo
		}
		esttab using "Table_03_a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_03) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear
	// non-G7 subsample
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
			qui: regxfe `x' zcci zd_* zF* if dum_G7 == 0, robust fe(id) cluster(year)
				eststo
		}
		esttab using "Table_03_b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_03) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear

	
* Table_04. Productivity and sentiment: Financial development
	clear all
	use "data/final_data_set.dta"
	xtset id year

	// build auxiliary data set
		*	do "do/auxiliary_files/data_construction_FD.do"

	// merge with main data set
		merge m:m country using "data/FD.dta"
		drop _merge
	// correlation between G7 countries and FD
		pwcorr dum_G7 fd1 fd2 fd3 fd4 fd0 if pd != ., sig obs
	// standardize
		************* here we do not subtract the mean because there is no time-series dimension
		foreach x in fd1 fd2 fd3 fd4 fd5 fd6 fd0 {
			egen `x'sd = sd(`x') if pd != .
			gen z`x' = `x' / `x'sd
			drop `x'sd 
		}	
	// create interaction terms
		foreach x in zcci zxb zres l_zpd zl_cci_ort zcci_ort zcci_exp {
			foreach y in fd1 fd2 fd3 fd4 fd5 fd6 fd0 {
				gen `x'_z`y' = `x' * z`y'
			}
		}
	// data availability per country
		tabulate country if zfd0 != .
		tabulate country if zfd1 != . // no observations before 1975
		tabulate country if zfd2 != . // no observations before 1975
		tabulate country if zfd3 != .
		tabulate country if zfd4 != .
		
	/* Here is the correspondence between numbers and types:
			gdp_per_capita_c fd0raw
			mkt_cap_to_gdp_c fd1raw
			stocks_traded_to_gdp_c fd2raw
			domestic_credit_to_gdp_c fd3raw
			bank_credit_to_gdp_c fd4raw
	*/

	// regressions
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
		foreach z in zfd0 {
			qui: regxfe `x' zcci `z' zcci_`z' zd_* zF*, robust fe(id) cluster(year)
				eststo
		}
		}
		esttab using "Table_04.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_04) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear
		
		
* Table_05. Sentiment and economic growth: G7 countries
	clear all
	use "data/final_data_set.dta"
	xtset id year
	generate newid = id
	tsset newid year

	// calculate t-step-ahead TFP growth
		foreach x in labsh d_labsh g_labsh {
			gen f_`x' = f.`x'
			gen f2_`x' = f2.`x'
			gen f3_`x' = f3.`x'
			gen f4_`x' = f4.`x'
			gen f5_`x' = f5.`x'
			gen f6_`x' = f6.`x'
			gen f7_`x' = f7.`x'
			gen f8_`x' = f8.`x'				
		}
	
	// Panel A. Capital formation
		foreach x in f_g_rgcf f2_g_rgcf f3_g_rgcf f4_g_rgcf {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
					eststo
			}
		esttab using "Table_05a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_05) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// Panel B. Labor income
		foreach x in f_labsh f2_labsh f3_labsh f4_labsh {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
					eststo
			}
		esttab using "Table_05b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_05) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// Panel C. Consumption
		foreach x in f_g_rhc f2_g_rhc f3_g_rhc f4_g_rhc {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
					eststo
			}
		esttab using "Table_05c.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_05) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// Panel D. Employment
		foreach x in f_g_emp f2_g_emp f3_g_emp f4_g_emp {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
					eststo
			}
		esttab using "Table_05d.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_05) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// Panel E. Real GDP 
		foreach x in f_g_rgdpna f2_g_rgdpna f3_g_rgdpna f4_g_rgdpna {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
					eststo
			}
		esttab using "Table_05e.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_05) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// Panel F. Productivity
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
					eststo
			}
		esttab using "Table_05f.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_05) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
				
				
* Table_06. Sentiment and economic growth: Non-G7 countries
	clear all
	use "data/final_data_set.dta"
	xtset id year
	generate newid = id
	tsset newid year

	// calculate t-step-ahead TFP growth
		foreach x in labsh d_labsh g_labsh {
			gen f_`x' = f.`x'
			gen f2_`x' = f2.`x'
			gen f3_`x' = f3.`x'
			gen f4_`x' = f4.`x'
			gen f5_`x' = f5.`x'
			gen f6_`x' = f6.`x'
			gen f7_`x' = f7.`x'
			gen f8_`x' = f8.`x'				
		}
	
	// Panel A. Capital formation
		foreach x in f_g_rgcf f2_g_rgcf f3_g_rgcf f4_g_rgcf {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
					eststo
			}
		esttab using "Table_06a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_06) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// Panel B. Labor income
		foreach x in f_labsh f2_labsh f3_labsh f4_labsh {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
					eststo
			}
		esttab using "Table_06b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_06) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// Panel C. Consumption
		foreach x in f_g_rhc f2_g_rhc f3_g_rhc f4_g_rhc {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
					eststo
			}
		esttab using "Table_06c.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_06) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// Panel D. Employment
		foreach x in f_g_emp f2_g_emp f3_g_emp f4_g_emp {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == ), fe
					eststo
			}
		esttab using "Table_06d.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_06) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// Panel E. Real GDP 
		foreach x in f_g_rgdpna f2_g_rgdpna f3_g_rgdpna f4_g_rgdpna {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
					eststo
			}
		esttab using "Table_06e.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_06) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// Panel F. Productivity
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
					eststo
			}
		esttab using "Table_06f.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_06) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear

		
* Table_07. Future stock returns
	clear all
	use "data/final_data_set.dta"
	xtset id year
	generate newid = id
	tsset newid year

	// full sample
		foreach x in f_ret f2_ret f3_ret f4_ret {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: xtreg `x' zcci_ort zcci_exp i.year, fe
				eststo
		}
		esttab using "Table_07_a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_07_a) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// G7 countries
		foreach x in f_ret f2_ret f3_ret f4_ret {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
				eststo
		}
		esttab using "Table_07_b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_07_b) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// Non-G7 countries
		foreach x in f_ret f2_ret f3_ret f4_ret {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
				eststo
		}
		esttab using "Table_07_c.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_07_c) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear		
		
		
* Table_08 Equity and credit markets
	clear all
	use "data/final_data_set.dta"
	xtset id year
	
	// merge with main data set
		merge m:m country year using "data/vFD.dta"
		drop _merge
		duplicates list id year
		duplicates drop id year, force
		tsset id year
		generate newid = id
		tsset newid year

	// correlation between G7 countries and FD
		pwcorr dum_G7 vfd1 vfd2 vfd3 vfd4 vfd0 if pd != ., sig obs
	// generate equity/debt ratios	
		gen vfd13 = vfd1 / vfd3	
		gen vfd14 = vfd1 / vfd4	
		gen vfd23 = vfd2 / vfd3	
		gen vfd24 = vfd2 / vfd4	
	// generate future values		
		foreach x in vfd1 vfd2 vfd3 vfd4 vfd13 vfd14 vfd23 vfd24 {
			gen f_`x' = f.`x'
			gen f2_`x' = f2.`x'
			gen f3_`x' = f3.`x'
			gen f4_`x' = f4.`x'
		}
	// check country availability
		tabulate country if vfd24 != . & zcci_ort != .
	// check period availability
		tabstat year if vfd24 != . & zcci_ort != ., statistics(min max)

	// full sample
		foreach x in f_vfd24 f2_vfd24 f3_vfd24 f4_vfd24 {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: xtreg `x' zcci_ort zcci_exp i.year, fe
				eststo
		}
		esttab using "Table_08_a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_08_a) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// G7 countries
		foreach x in f_vfd24 f2_vfd24 f3_vfd24 f4_vfd24 {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
				eststo
		}
		esttab using "Table_08_b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_08_b) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// non-G7 countries
		foreach x in f_vfd24 f2_vfd24 f3_vfd24 f4_vfd24 {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
				eststo
		}
		esttab using "Table_08_c.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_08_c) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
		
		
